﻿/*
 * Created by SharpDevelop.
 * User: Y-Windows
 * Date: 2012/03/23
 * Time: 7:41
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace LinBoxDotNet
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public static class GLOBAL{
		public static string plid = "0";
		public static string nurl = "http://www.youtube.com/v/WSt7NTFbWZw?autoplay=1&amp;f=videos&amp;app=youtube_gdata";
		public static int nsec = 260;
	}
	public partial class MainForm : Form
	{
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		void Button1Click(object sender, EventArgs e)
		{
			if(textBox1.Text != ""){
				button1.Enabled = false;
				textBox1.Enabled = false;
				string escaped = Uri.EscapeUriString(textBox1.Text);
				string url = "http://linsgamecircle.appspot.com/?q=" + escaped;
				webBrowser2.Navigate(url);
			}else{
				
			}
		}
		
		void WebBrowser2DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
		{
			button1.Enabled = true;
			textBox1.Enabled = true;
			textBox1.Text = "";
		}
		
		void WebBrowser1DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
		{
			webBrowser3.Refresh();
		}
		void LoadNextMovie(string url){
			webBrowser1.Navigate(url);
		}
		
		void BackgroundWorker1DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
		{
			timer1.Stop();
			String URLString = "http://blooming-sunset-7188.heroku.com/next?b=" + GLOBAL.plid;
			XmlTextReader reader = new XmlTextReader (URLString);
			int flag = 0;
			bool fff = false;
			while (reader.Read()) {
			  	switch (reader.NodeType) {
        			case XmlNodeType.Element: // ノードはエレメントです。
						if(reader.Name == "id"){
							flag = 1;
							fff = true;
						}else if(reader.Name == "url"){
							flag = 2;
						}else if(reader.Name == "sec"){
							flag = 3;
						}
            			break;
	
        			case XmlNodeType.Text: // 各エレメントのテキストを表示します。
            			switch(flag){
            				case 1:
            					GLOBAL.plid = reader.Value;
            					flag = 0;
            					break;
            				case 2:
            					GLOBAL.nurl = reader.Value;
            					flag = 0;
            					break;
            				case 3:
            					GLOBAL.nsec = int.Parse(reader.Value);
            					flag = 0;
            					break;
            			}
            			
            			break;

        			case XmlNodeType. EndElement: // エレメントの最後を表示します。
            			Console.Write("</" + reader.Name);
          				Console.WriteLine(">");
            			break;
 				}
			}
			
			string nurl = "http://www.youtube.com/v/WSt7NTFbWZw?autoplay=1&amp;f=videos&amp;app=youtube_gdata";
			int nsec = 262 * 1000;
			if(fff){
				nurl = GLOBAL.nurl;
				nsec = GLOBAL.nsec * 1000 + 2000;
			}
			this.Invoke(new MethodInvoker(delegate { 
			                              	webBrowser1.Navigate(nurl.Replace("version=3","autoplay=1"));
			                              	timer1.Interval = nsec;
			                              	timer1.Start();
			                              }));
		}
		void Timer1Tick(object sender, EventArgs e)
		{
			backgroundWorker1.RunWorkerAsync();
		}
		
		void MainFormLoad(object sender, EventArgs e)
		{
			timer1.Start();
		}
		
		void 終了ToolStripMenuItemClick(object sender, EventArgs e)
		{
			this.Close();
		}
		
		void スキップToolStripMenuItemClick(object sender, EventArgs e)
		{
			backgroundWorker1.RunWorkerAsync();
		}
		
		void Button2Click(object sender, EventArgs e)
		{
			textBox2.Enabled = false;
			button2.Enabled = false;
			webBrowser4.Navigate("http://blooming-sunset-7188.heroku.com/direct?v=" + textBox2.Text);
		}
		
		void WebBrowser4DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
		{
			textBox2.Text = "";
			textBox2.Enabled = true;
			button2.Enabled = true;
			
		}
	}
}
